<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/4/12
 * Time: 14:53
 */

namespace console\controllers;
use services\models\Comments;
use services\models\CommentsReply;
use services\models\CommentsRelation;
use common\models\UpFileData;
use Yii;


/*
 * member_no 与 bestry_uid 的关系参照iliketry.51golife_bind_info表
 * 喜试order表的no，product_no,spec_no，加2000W 对应到order_id,goods_id,product_id
 * php yii migrate-liketry/index 4 100 20000000
 * */

class MigrateLiketryController extends \yii\console\Controller{

    public function actionIndex($start,$end,$comment_id){

        $add = 20000000;
/*        $start = 5498009;
        $end = 5588400;
        $comment_id = 20007521;*/

        for($no = $start; $no < $end; $no++) {
            $article = Yii::$app->db->createCommand('select * from iliketry.product_article WHERE no = ' . $no);
            $articleData = $article->queryOne();


            if (!empty($articleData)) {
                $member = Yii::$app->db->createCommand('select uid from tmall1.member_info WHERE lkt_uid = ' . $articleData['member_no']);
                $memberData = $member->queryOne();

                if (!empty($memberData)) {
                    $bestry_uid = $memberData['uid'];
                } else {
                    var_dump('ilketry bestry_uid');
                    continue;
                }
                //查订单表
                $order = Yii::$app->db->createCommand('select * from iliketry.order WHERE no = '. $articleData['order_no']);
                $orderData = $order->queryOne();

                if (!empty($orderData)) {
                    $order_id = $orderData['no'] + $add;
                    $goods_id = $orderData['product_no'] + $add;
                    $product_id = $orderData['spec_no'] + $add;
                } else {
                    //查order_last表
                    $order_last = Yii::$app->db->createCommand('select * from iliketry.order_last WHERE NO =' . $articleData['order_no']);
                    $lastData = $order_last->queryOne();
                    if(!empty($lastData)){
                        $order_id = $lastData['no'] + $add;
                        $goods_id = $lastData['product_no'] + $add;
                        $product_id = $lastData['spec_no'] + $add;
                    }
                    else{
                        $product_spec = Yii::$app->db->createCommand('select * from iliketry.product_spec WHERE product_no = '. $articleData['product_no']);
                        $specData = $product_spec->queryOne();
                        if(!empty($specData)){
                            $order_id = 0;
                            $goods_id = $specData['product_no'] + $add;
                            $product_id = $specData['no'] + $add;
                        }
                        else{
                            var_dump('iliketry goods_id');
                            continue;
                        }
                    }
                }
                if($articleData['add_time'] < '2008-01-01 12:22:11'){
                    $articleData['add_time'] = '2008-01-01 08:00:00';
                }

                $imgStr = '';
                for ($i = 1; $i <= 3; $i++) {//图片地址处理
                    if (!empty($articleData['pic' . $i])) {

                        $img = explode('./', $articleData['pic' . $i]);
                        if (is_numeric(strpos($articleData['pic' . $i], './'))) {
                            $imgPath = $img[1];
                        } else {
                            $imgPath = $img[0];
                        }
                        $imgP = explode('/',$imgPath);
                        if(count($imgP) == 1){//不存在目录结构的图片移到before目录
                            $imgPath = 'before/'.$imgPath;
                        }
                        $tile = explode('/', $imgPath);
                        $tileName = $tile[count($tile) - 1];

                        $imgArr = array(
                            'c_uri' => $imgPath,
                            'uid' => $articleData['member_no'],
                            'db_time' => $articleData['add_time'],
                            'title' => $tileName,
                        );

                        $upImg = new UpFileData($imgArr);
                        $upImg->save();
                        if (empty($imgStr)) {
                            $imgStr .= $upImg->id;
                        } else {
                            $imgStr .= ',' . $upImg->id;

                        }
                    }
                }
                $content = explode('|*@', $articleData['content']);
                //填入comments表的数据
                $commentsData = array(
                    'comment_id' => $comment_id,
                    'content' => $content[0],
                    'img' => $imgStr,
                    'create_time' => $articleData['add_time'],
                );

                $modelC = Comments::findOne($comment_id);
                if (empty($modelC)) {
                    $modelC = new Comments($commentsData);
                    $modelC->save();

                } else {
                    //var_dump($comment_id);
                    //var_dump('liketry comment_id');
                    //die;
                    $modelC->load($commentsData, '');
                    $modelC->save();
                }

                $is_reply = 0;
                $reply_id = 0;
                if (isset($content[1]) && !empty($content[1])) {//回复数据处理
                    $replyArr = explode('&*$', $content[1]);
                    $replyContent = isset($replyArr[0]) ? $replyArr[0] : '';
                    $replyTime = isset($replyArr[1]) ? $replyArr[1] : '';

                    if (!empty($replyContent) && !empty($replyTime)) {
                        //填入comments_reply表的数据
                        $replyData = array(
                            'comment_id' => $comment_id,
                            'content' => $replyContent,
                            'create_time' => $replyTime,
                            'update_time' => $replyTime,
                            'admin_id' => 0,
                        );

                        $modelR = CommentsReply::findOne(['comment_id' => $comment_id]);
                        if (empty($modelR)) {
                            $modelR = new CommentsReply($replyData);
                            $modelR->save();

                        } else {
                            var_dump($modelR->reply_id);
                            var_dump('liketry reply_id');
                            //die;
                            $modelR->load($replyData, '');
                            $modelR->save();
                        }

                        $is_reply = 1;
                        $reply_id = $modelR->reply_id;
                    }
                }


                $is_try = $articleData['type'] == 2 ? 0 : 1;
                //$status = in_array($articleData['status'], array('1', '2', '3')) ? ($articleData['status'] - 1) : -1;
                $status = in_array($articleData['status'], array('1', '2', '3')) ? ($articleData['status'] - 1) : 0;//待审核状态的心得设为通过
                $relationData = array(
                    'comment_id' => $comment_id,
                    'order_id' => $order_id,
                    'goods_id' => $goods_id,
                    'product_id' => $product_id,
                    'bestry_uid' => $bestry_uid,
                    'level' => 5,
                    'create_time' => $articleData['add_time'],
                    'is_try' => $is_try,
                    'is_reply' => $is_reply,
                    'reply_id' => $reply_id,
                    'is_show' => 1,
                    'status' => $status,
                );


                $relationC = CommentsRelation::findOne(['comment_id' => $comment_id]);
                if (empty($relationC)) {
                    $relationC = new CommentsRelation($relationData);
                    $relationC->save();
                } else {
                    //var_dump($relationData);die;
                    $relationC->load($relationData, '');
                    $relationC->save();
                }

                $comment_id++;
            }
        }
    }

    public function actionTest(){
        $val['content'] = '很不错的妮维雅护手霜，是我喜欢的味道，很赞|*@123abc&*$2016-04-13 16:22:10';
        $content =explode('|*@', $val['content']);
        var_dump($content);
        if(isset($content[1]) && !empty($content[1])){
            $reply = $content[1];
            $replyArr = explode('&*$', $reply);
            var_dump($replyArr);
        }
    }

}